<template>
	<view class="all_body">
         <view class="bady_header">
			<view class="input_baby"> <input placeholder="搜索" v-model="inputValue" confirm-type="search" @confirm="search()" ></view>
			 <view class="input_cancle" @click="returnBabyFriend()">取消</view>
		 </view>
		
		<view class="baby_container" >
			<view class="baby_container_item" v-for="item in friendInfoShow" :key="item._id" @click="toChatPage(item.us)">
				<view class="friend_img"><image :src="imgSrc" mode="widthFix" ></image></view>
				<view class="friend_container" >
					<view class="friend_container_name">{{item.us}}</view>
					<view class="friend_container_text">{{lastChatText}}</view>
				</view>
				
				<view class="friend_container_right">
					<view class="friend_container_time" v-if="isBabyFriend">下午 5:37</view>
					<view class="friend_container_button" @click="addFriend()"><button type="primary" size="mini" v-if="!isBabyFriend" >添加</button></view>
				</view>
				
			</view>
			
			
			
			
		</view>

		
	</view>
</template>

<script>
	import request from '../../static/utils/request.js'
	export default {
		data(){
			return {
				inputValue:'',
				isBabyFriend:true,
				username:'',
				friendList:[],
				friendInfoShow:[],
				mail:'',
				isAggreeFriend:[],
				friendTokenTime:'',
				lastChatText:'',
				
				
			}
		},
		methods:{
		async	search(){
				let that = this
			
		    if(that.inputValue.trim().length!==0){
				let parmas = {mail:this.inputValue}
				let res = await request("POST","/findUser",parmas)
				console.log(res)
				if(that.friendList.length!=0&&res.data.err==0){
					that.friendList.forEach(item=>{
						if(item.friendname==res.data.data[0].us){
									that.isBabyFriend = true
						}else{
							uni.setStorageSync("babyFriend",that.friendInfoShow)
							that.friendInfoShow = []
							that.friendInfoShow.push(res.data.data[0])
							that.isBabyFriend = false
						}
					})
				}else{
					if(res.data.err==0){
							that.friendInfoShow = []
						that.friendInfoShow.push(res.data.data[0])
						that.isBabyFriend = false
					}
					
				}
			}
		
			
			},
			babyFriend(){
				let that = this
				let parmas = {friendTokenTime:that.friendTokenTime,username:that.username}
				let friendname = ''
				let friendmail = ''
				console.log(parmas)
				uni.request({
					url:"http://localhost:4008/bady/babyHaveFriend",
					method:"POST",
					data:parmas,
					success(res) {
						console.log(888888888888,res.data.data)
					    res.data.data.forEach(item=>{
						  that.friendList.push({friendname:item.friend,friendmail:item.mail})
						  that.isBabyFriend = true
						  that.friendInfoShow.push({us:that.username==item.username?item.friend:item.username,_id:item.friendTokenTime})
						})
						
					}
					
				})
			},
			haveFriendData(){
				let that = this 
				let  parmas
			
			      setTimeout(()=>{
					   	console.log(that.friendList)
					  if(this.friendList.length!==0){
					  					 this.friendList.forEach(item=>{
					  					 console.log(item)
					  					 parmas = {mail:item.friendmail}
					  					 console.log(parmas)
					  					 uni.request({
					  					 	url:"http://localhost:4008/user/findUser",
					  					 	method:"POST",
					  					 	data:parmas,
					  					 	success(res) {
					  					 		console.log(res)
												that.friendInfoShow = res.data.data
												uni.setStorageSync("babyFriend",that.friendInfoShow)
					  					 	}
					  					 })
					  					 
					  					 })
					  }
				  },100)
				},
				returnBabyFriend(){
					let that = this
					// that.friendInfoShow = []
					that.isBabyFriend = true
					that.inputValue = ''
			        uni.getStorage({
			        	key:"babyFriend",
						success(res) {
							console.log(res)
							that.friendInfoShow = res.data
						}
			        })
				},
				addFriend(){
					let that = this
					let date = Date.now()
					that.friendTokenTime = date
					let parmas = {
						username:that.username,
						friendTokenTime:date,
						friednname:that.friendInfoShow[0].us,
						friendmail:that.friendInfoShow[0].mail,
						isRead:false,
						mymail:that.mail
					}
					console.log(parmas)
					uni.request({
						url:"http://localhost:4008/babydataShow/insert",
						method:"GET",//uniapp中的get请求不需要params vue需要
						data:parmas,
						success(res) {
                              console.log(res)  
							 //  uni.setStorage({
							 //  	key:"friendTokenTime",
								// data:date
							 //  })
						}
					})	
				},
				//是否同意好友请求
				isAgreeRequest(){
					let that = this
					uni.request({
						url:"http://localhost:4008/friendInfomation.json",
						method:"GET",
						success(res) {
							console.log(res)
							res.data.forEach(item=>{
								if(that.username==item.username&&item.isRead=='true'){
									that.isAggreeFriend.push(item)
									console.log(item)
									console.log(item.friednname)
									that.insertfriendTokenTime(item.friednname,item.friendTokenTime)
								
									let parmas = {username:that.username,friend:item.friednname,mail:item.friendmail,friendTokenTime:item.friendTokenTime}
									console.log(parmas)
									uni.request({
										url:"http://localhost:4008/bady/insertBabyFriend",
										method:"POST",
										data:parmas,
										success(ret) {
											console.log(ret)	
										}
									})
									
												
								}	
							})
						}
					})	
				},
				
				// requestfriendTokenTime(){
				// 	console.log(12346)
				// 	uni.request({
				// 		url:"http://localhost:4008/friendTokenTime.json",
				// 		method:"GET",
				// 		success(res) {
				// 			console.log(res)
				// 		}
				// 	})
				// },
				insertfriendTokenTime(friendname,friendTokenTime){
					let parmas1 = {username:this.username,friendTokenTime:friendTokenTime}
					let parmas2 = {username:friendname,friendTokenTime:friendTokenTime}
					// let parmas2 = {username:this.isAggreeFriend,friendTokenTime:this.friendTokenTime}
					uni.request({
						url:"http://localhost:4008/key/insert",
						method:"GET",
						data:parmas1,
						success(res) {
							  console.log(res)
						}
					})
					
					
					uni.request({
						url:"http://localhost:4008/key/insert",
						method:"GET",
						data:parmas2,
						success(res) {
							  console.log(res)
							  res.forEach(item=>{
								  // if(item.username){}
								  
							  })
						}
					})
				},
				
				
				requestfriendTokenTime(){
					let that = this
					uni.request({
						url:"http://localhost:4008/friendTokenTime.json",
						method:"GET",
						success(res) {
						
							res.data.forEach(item=>{
								// console.log(item.friendTokenTime)
								if(item.username==that.username){
									that.friendTokenTime = item.friendTokenTime
								}
							})
						}
					})
					
					
					
					
					
					
				},
				toChatPage(e){
				 if(this.isBabyFriend){
					 console.log(e)
					 uni.navigateTo({
					 	url:"./chatWithFriend?friend="+e
					 })
				 }
				},
				lastChatWithFriend(){
				let data =	uni.getStorageSync("chatText")
				console.log(data)
				if(data.length>0){
					let length = data.length
					this.lastChatText = data[length-1].msg
				}
					
				}
				
				
			
		},
		onLoad() {
	
			let that = this
			that.username = uni.getStorageSync("user")[0].us
			that.mail = uni.getStorageSync("user")[0].mail
			// that.friendTokenTime = uni.getStorageSync("friendTokenTime")
			that.requestfriendTokenTime()
			this.isAgreeRequest()
				setTimeout(()=>{this.babyFriend()},500)
			this.haveFriendData()
            this.lastChatWithFriend()
			

			
			
		},
		computed:{
			imgSrc(){
				if(this.username=="孙一飞"){
					return '../../static/image/cat1.jpeg'
				}else{
					return '../../static/image/cat.jpeg'
				}
			}
		}
	}
</script>

<style lang="less">
	.all_body{
		background: #ccc3;
	}
	.bady_header{
		width: 80%;
		height: 70rpx;
	    margin: auto;
		display: flex;
		justify-content: space-around;
		align-items: center;
		.input_baby{
			width: 80%;
			input{
				font-family: "楷体";
				font-size: 30rpx;
				width: 100%;
				height: 100%;
				text-align: center;
				border: 1rpx solid #fff;
			    background: #fff;
				border-radius: 50rpx;
				
			}
		}
		.input_cancle{
			color: #0087ff;
			font-family: "楷体";
		}
	   
	}
	
	.baby_container{
		width: 90%;
		height: 100vh;
		margin: auto;
		.baby_container_item{
			width: 100%;
			height: 200rpx;
			display: flex;
			// justify-content: center;
			align-items: center;
			font-size: 30rpx;
			font-family: "楷体";
			border-bottom: 1rpx soid #fff;
			.friend_img{
				flex: 1;
				image{
					width: 100rpx;
					border-radius: 50%;
				}
			}
			.friend_container{
				flex: 4;
				display: flex;
				flex-direction: column;
				justify-content: space-around;
				.friend_container_name{
					
				}
				.friend_container_text{
					
				}
			
			}
			.friend_container_right{
				display: flex;
				flex-direction: column;
				justify-content: space-around;
				.friend_container_time{}
				.friend_container_button{
					button{
				        border-radius: 50rpx;
						margin-top: 20rpx;
					
					}
				}
			}
			
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
</style>
